package com.spring.study.bean.definition;

import com.spring.study.bean.factory.UserFactory;
import com.spring.study.bean.factory.UserFactoryImpl;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SingletonBeanResDemo {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //创建一个外部对象
        UserFactory userFactory = new UserFactoryImpl();
        ConfigurableBeanFactory beanFactory = applicationContext.getBeanFactory();
        //注册外部单体bean
        beanFactory.registerSingleton("userFactory",userFactory);
        //启动上下文
        applicationContext.refresh();
        //  依赖查找
        UserFactory userFactoryByLookUp = beanFactory.getBean("userFactory",UserFactory.class);
        System.out.println(userFactory == userFactoryByLookUp);
        //关闭上下文
        applicationContext.close();
        System.out.println("spring应用上下文关闭》》》");
    }
}
